package com.apexwear.apexutils.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.apexwear.apexutils.R;
import com.apexwear.apexutils.adapter.AdapterMultiDialog;
import com.apexwear.apexutils.api.OnPositiveClickListener;
import com.apexwear.apexutils.model.MultiData;
import com.apexwear.apexutils.model.MultiSelect;
import com.apexwear.apexutils.utils.ShowToast;

import java.util.ArrayList;
import java.util.List;

public class MultiDialog extends Dialog {
    Context mContext;
    OnPositiveClickListener onPositiveClickListener;
    List<MultiSelect> multiList;
    String title;
    AdapterMultiDialog adapterMultiDialog;

    public void setOnPositiveClickListener(OnPositiveClickListener onPositiveClickListener) {
        this.onPositiveClickListener = onPositiveClickListener;
    }

    public MultiDialog(@NonNull Context context, String[] dataList, String title, int defaultSelect) {
        super(context, R.style.waiteDialog);
        mContext = context;
        this.title = title;
        multiList = new ArrayList<>();
        for (int i = 0; i < dataList.length; i++) {
            MultiSelect multiSelect = new MultiSelect();
            multiSelect.setTxt(dataList[i]);
            if (defaultSelect == i) {
                multiSelect.setSelect(true);
            } else {
                multiSelect.setSelect(false);
            }
            multiList.add(multiSelect);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View dialogView = LayoutInflater.from(mContext).inflate(R.layout.dialog_cycle, null, false);
        RecyclerView recyclerView = dialogView.findViewById(R.id.cycle_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        adapterMultiDialog = new AdapterMultiDialog(multiList, mContext);
        recyclerView.setAdapter(adapterMultiDialog);
        setContentView(dialogView);
        AppCompatButton sureButton = dialogView.findViewById(R.id.sure);
        sureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                String txt = "";
                int cycle = 0;
                for (int i = 0; i < multiList.size(); i++) {
                    if (multiList.get(i).isSelect()) {
                        String currentTxt = multiList.get(i).getTxt();
                        txt += (currentTxt + " ");
                        if (currentTxt.equalsIgnoreCase(mContext.getString(R.string.cycle_1))) {
                            cycle = (cycle | 2);
                        } else if (currentTxt.equalsIgnoreCase(mContext.getString(R.string.cycle_2))) {
                            cycle = (cycle | 4);
                        } else if (currentTxt.equalsIgnoreCase(mContext.getString(R.string.cycle_3))) {
                            cycle = (cycle | 8);
                        } else if (currentTxt.equalsIgnoreCase(mContext.getString(R.string.cycle_4))) {
                            cycle = (cycle | 16);
                        } else if (currentTxt.equalsIgnoreCase(mContext.getString(R.string.cycle_5))) {
                            cycle = (cycle | 32);
                        } else if (currentTxt.equalsIgnoreCase(mContext.getString(R.string.cycle_6))) {
                            cycle = (cycle | 64);
                        } else if (currentTxt.equalsIgnoreCase(mContext.getString(R.string.cycle_7))) {
                            cycle = (cycle | 1);
                        } else if (currentTxt.equalsIgnoreCase(mContext.getString(R.string.cycle_8))) {
                            cycle = (cycle | 0);

                        }
                    }
                }
                if (TextUtils.isEmpty(txt)) {
                    ShowToast.show(mContext, getContext().getString(R.string.cycle_time_hint));
                    return;
                }
                MultiData multiData = new MultiData();
                if (txt.contains(" ")) {
                    String[] arrayString = txt.split(" ");
                    if (arrayString.length == 7) {
                        txt = mContext.getString(R.string.every_day);
                    }
                }
                multiData.setTxt(txt);
                multiData.setCycle(cycle);
                if (onPositiveClickListener != null) {
                    onPositiveClickListener.clickButton(multiData);
                }

            }
        });

    }

    public void destroyDialog() {
        dismiss();
        if (adapterMultiDialog != null) {
            adapterMultiDialog.destroyAdapter();
            adapterMultiDialog = null;
        }
        mContext = null;
        onPositiveClickListener = null;
        multiList = null;
    }
}
